package com.example.neteasecloudmusic.ui.song_list_detail;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.SongDetailInfo;
import com.example.neteasecloudmusic.bean.playListBean.Tracks;
import com.example.neteasecloudmusic.greenDaoGen.SongDetailController;
import com.example.neteasecloudmusic.greenDaoGen.SongDetailInfoDao;
import com.example.neteasecloudmusic.ui.PlayMusicActivity;

import java.util.List;

public class RvUpAdapter extends RecyclerView.Adapter<RvUpAdapter.RvViewHolder> implements View.OnClickListener {
    private List<SongDetailInfo> songDetailInfos;
    private Integer index;
    private OnItemClickListener listener;
    private RecyclerView recyclerView;
    private SongDetailController mSongDetailController;

    public RvUpAdapter(List<SongDetailInfo> songDetailInfos, int index, RecyclerView rv) {
        this.songDetailInfos = songDetailInfos;
        this.index = index;
        this.recyclerView = rv;
    }

    @NonNull
    @Override
    public RvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_rv_list, parent, false);
        itemView.setOnClickListener(this);
        return new RvViewHolder(itemView);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RvViewHolder holder, int position) {
        SongDetailInfo songDetailInfo = songDetailInfos.get(position);
        holder.singer.setText(songDetailInfo.getSingerName());
        holder.song.setText(songDetailInfo.getSongName());
        if (index == position) {
            holder.singer.setTextColor(R.color.red);
            holder.song.setTextColor(R.color.red);
        }

        /**
         * todo: 歌单列表删除，有问题
         */
        int pos = position;
        holder.iv_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyItemRemoved(pos);
                notifyItemRangeChanged(pos, songDetailInfos.size());
                removeSongClick(songDetailInfo);
            }
        });
    }


    @Override
    public int getItemCount() {
        return songDetailInfos.size();
    }

    public void setOnItemClickListener (OnItemClickListener listener) {
        this.listener = listener;
    }

    public void removeSongClick(SongDetailInfo songDetailInfo) {
        if (listener != null) {
            listener.onRemoveSongClick(songDetailInfo);
        }
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            int p = recyclerView.getChildAdapterPosition(v);
            listener.onItemClick(p);
        }
    }

    class RvViewHolder extends RecyclerView.ViewHolder {
        TextView singer, song, song_num;
        ImageView iv_remove;
        RvViewHolder(@NonNull View itemView) {
            super(itemView);
            singer = itemView.findViewById(R.id.rv_up_singer);
            song = itemView.findViewById(R.id.rv_up_song);
            iv_remove = itemView.findViewById(R.id.ic_remove);
            song_num = itemView.findViewById(R.id.tv_song_num);
        }
    }

    public interface OnItemClickListener {
        void onItemClick (int position);

        void onRemoveSongClick (SongDetailInfo songDetailInfo);

    }

}
